<?php
// 应用公共文件

/**
 * 私钥、公钥加密
 * @param $data
 * @param $type
 * @return string
 */
function sslEn($data, $type = 'pu')
{
    global $private_key, $public_key;
    $encrypted = "";
    if ($type == 'pi') {
        //私钥加密
        $pi_key = openssl_pkey_get_private(config('rsa_private'));
        openssl_private_encrypt($data, $encrypted, $pi_key);
    } elseif ($type == 'pu') {
        //公钥加密
        $pu_key = openssl_pkey_get_public(config('rsa_public'));
        openssl_public_encrypt($data, $encrypted, $pu_key);
    }
    $encrypted = base64_encode($encrypted);
    return $encrypted;
}

/**
 * 私钥、公钥解密
 * @param $data
 * @param $type
 * @return string
 */
function sslDe($data, $type = 'pi')
{
    global $private_key, $public_key;
    $decrypted = "";
    if ($type == 'pu') {
        //公钥解密
        $pu_key = openssl_pkey_get_public($public_key);
        openssl_public_decrypt(base64_decode($data), $decrypted, config('rsa_public'));
    } elseif ($type == 'pi') {
        //私钥解密
//        $pi_key = openssl_pkey_get_private($private_key);
//        openssl_private_decrypt(base64_decode($data), $decrypted, config('rsa_private'));
        //私钥解密  ---- 分段解密
        $arrThrunk = str_split(base64_decode($data), 256);
        foreach ($arrThrunk as $trunk)
        {
            $temp = '';
            if (openssl_private_decrypt($trunk, $temp, config('rsa_private'))) {
                $decrypted	.= $temp;
            } else {
                return '';
            }
        }
    }
    return $decrypted;
}

/**
 * 跨域处理
 */

function origin()
{

    $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';

     $allow_origin = array(
         'http://localhost:8081',
         'http://localhost',
        'https://dev-center.ishaohuo.cn',
        'https://dev-admin.ishaohuo.cn',
        'https://admin_shaohuo.ishaohuo.cn',
    );
    if (in_array($origin, $allow_origin)) {
        header('Access-Control-Allow-Origin:' . $origin);
        header('Access-Control-Allow-Headers: access_token,content-type,copyright,interface');
        header('Access-Control-Allow-Method: POST');
    } else {
        die('Invalid Access');
    }
}

/**
 * 调用打印 并停止进程
 * @param $var
 */
function pre($var)
{
    echo '<pre>';
    print_r($var);
    echo '</pre>';
    exit;
}

/**
 * 打印不停止进程
 * @param $var
 */
function pr($var)
{
    echo '<pre>';
    print_r($var);
    echo '</pre>';
}

//创建TOKEN
function creatToken()
{
    $code = chr(mt_rand(0xB0, 0xF7)) . chr(mt_rand(0xA1, 0xFE)) . chr(mt_rand(0xB0, 0xF7)) . chr(mt_rand(0xA1, 0xFE)) . chr(mt_rand(0xB0, 0xF7)) . chr(mt_rand(0xA1, 0xFE));
    return authcode($code);
}

/* 加密TOKEN */
function authcode($str)
{
    $key = "ANDIAMON";
    $str = substr(md5($str), 8, 10);
    return md5($key . $str);
}


/**
 * 生成TOKEN值
 *
 */
function GenToken($size = 32)
{
    $arr = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@";
    $len = strlen($arr);
    $str = '';
    for ($i = 0; $i < $size; $i++) {
        $str .= $arr[rand(0, $len - 1)];
    }
    return $str;
}


function gmt_iso8601($time) {
    $dtStr = date("c", $time);
    $mydatetime = new DateTime($dtStr);
    $expiration = $mydatetime->format(DateTime::ISO8601);
    $pos = strpos($expiration, '+');
    $expiration = substr($expiration, 0, $pos);
    return $expiration."Z";
}

/**
 * 生成订单号
 */
function GenOrderId()
{
    //测试使用
    return 'SH'.time();
}

function make_tree($list,$pk='sm_id',$pid='pid',$child='children',$root=0){
    $tree=array();
    foreach($list as $key=> $val){
        if($val[$pid]==$root){
            //获取当前$pid所有子类
            unset($list[$key]);
            if(! empty($list)){
                $child=make_tree($list,$pk,$pid,$child,$val[$pk]);

                if(!empty($child)){
                    $val['_child']=$child;
                }
            }
            $tree[]=$val;
        }
    }
    return $tree;
}


function WLog($name = '', $a)
{
    $filename = empty($name) ? 'pay_log.txt' : $name;
    $word = $a;  //双引号会换行 单引号不换行
    file_put_contents($filename, $word . "\r\n", FILE_APPEND);
}
